<?php

declare(strict_types=1);

namespace app\admin\controller;

use think\Request;
use app\admin\service\ProductPrice as ProductPriceService;
use app\admin\validate\ProductPrice as ProductPriceValidate;
use think\facade\Db;

class ProductPrice extends Base
{

    protected $service;
    protected $validate;
    protected $request;
    /**
     * @describe
     * @author: Bobo
     * @date: 2025-06-12 10:33:42 
     * @param [type] @required
     * @return Renderable
     */
    public function __construct(Request $request)
    {
        $this->service = new ProductPriceService();
        $this->validate = new ProductPriceValidate();
        $this->request = $request;
    }

    /**
     * 显示编辑资源列表.
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function index()
    {
        $params = $this->request->params;
        if (!$this->validate->scene('search')->check($params)) {
            return $this->returnJsonData(100001, null, $this->validate->getError());
        }


        $pageIndex  = isset($params['pageIndex'])   ? intval($params['pageIndex']) : 1;
        $pageSize   = isset($params['pageSize'])    ? intval($params['pageSize']) : 10;
        $product_name = isset($params['product'])   ? $params['product'] : null;
        $is_futures = (isset($params['is_futures']) && $params['is_futures'] !== '') ? intval($params['is_futures']) : null;
        $start_date = isset($params['start_date'])   ? $params['start_date'] : null;
        $end_date = isset($params['end_date'])   ? $params['end_date'] : null;
        $real_arrival_date = isset($params['real_arrival_date'])   ? $params['real_arrival_date'] : null;
        $warehouse_id = isset($params['wid'])   ? $params['wid'] : null;

        $where[] = ['i.is_del', '=', 0];
        if ($product_name) {
            $pids = Db::name('product')->where(function ($query) use ($product_name) {
                $query->where('cname', 'like', '%' . $product_name . '%')->whereOr('abbr', 'like', '%' . $product_name . '%');
            })->where('is_del', 0)->column('id');
            $where[] = ['i.productid', 'in', $pids];
        }


        if ($is_futures !== null) {
            $where[] = ['i.is_futures', '=', $is_futures];
        }

        if ($start_date) {
            $where[] = ['i.arrival_date', '>=', $start_date];
        }

        if ($end_date) {
            $where[] = ['i.arrival_date', '<', $end_date];
        }
        if ($real_arrival_date) {
            $real_arrival_date = explode(',', $real_arrival_date);
            $where[] = ['i.real_arrival_date', 'between', $real_arrival_date];
        }

        if ($warehouse_id) {
            $where[] = ['i.wid', 'find in set', $warehouse_id];
        }
        $field = '*';

        $res =  $this->service->getList($pageIndex, $pageSize, $where, $field);

        return $this->returnJsonData($res['code'] ?? 0, $res['data'] ?? null, $res['message'] ?? '');
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
        $params = $this->request->params;
        if (!$params['id']) {
            return $this->returnJsonData(100001, null, 'id is required');
        }
        if ($params['is_futures'] < 0) {
            return $this->returnJsonData(100001, null, 'is_futures is required');
        }
        $res = $this->service->read($params);
        return $this->returnJsonData($res['code'] ?? 0, $res['data'] ?? null, $res['message'] ?? '');
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request)
    {
        $params = $this->request->params;
        // if (!$this->validate->scene('update')->check($params)) {
        //     return $this->returnJsonData(100001, null, $this->validate->getError());
        // }
        $params['creator'] = $this->request->operator_id;
        $res = $this->service->update($params);
        return $this->returnJsonData($res['code'] ?? 0, $res['data'] ?? null, $res['message'] ?? '');
    }

    /**
     * @describe 更新价格和折扣
     * @author: Bobo
     * @date: 2025-06-17 10:28:46 
     * @param [type] @required
     * @return Renderable
     */
    public function updatePriceAndDiscount()
    {
        $params = $this->request->params;
        $params['creator'] = $this->request->operator_id;
        $res = $this->service->updatePriceAndDiscount($params);
        return $this->returnJsonData($res['code'] ?? 0, $res['data'] ?? null, $res['message'] ?? '');
    }
}
